/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tansu_internal.h"
#include <wx/wx.h>
#include <wx/image.h>

#ifndef GENAFRAME_H
#define GENAFRAME_H

// begin wxGlade: ::dependencies
#include <wx/splitter.h>
#include <wx/listctrl.h>
#include <wx/notebook.h>
// end wxGlade

// begin wxGlade: ::extracode
enum {
    ID_GENA_RESET = 300,
    ID_GENA_SUB,
    ID_GENA_RESUB,
    ID_GENA_UNSUB,
    ID_GENA_EVENT_LIST,
    ID_GENA_VAR_LIST,
    ID_GENA_INSTANCE,
    ID_GENA_ATTRIBUTE,
};
// end wxGlade

#include <wx/listctrl.h>
#include "UpnpFrame.h"
#include "SoapFrame.h"
#include "GenaEvent.h"

class GenaFrame: public wxFrame {
public:
    // begin wxGlade: GenaFrame::ids
    // end wxGlade

    GenaFrame(struct event_base *evbase,
              const struct tansu_cp_device *cp_device,
              const struct tansu_cp_service *cp_service,
              wxWindow* parent,
              int id,
              const wxString& title,
              const wxPoint& pos=wxDefaultPosition,
              const wxSize& size=wxDefaultSize,
              long style=wxDEFAULT_FRAME_STYLE);

    virtual ~GenaFrame();

    enum GenaSubType {
        GENA_SUB_TYPE_SUBSCRIBE,
        GENA_SUB_TYPE_UNSUBSCRIBE,
        GENA_SUB_TYPE_RESUBSCRIBE,
    };

private:
    // begin wxGlade: GenaFrame::methods
    void set_properties();
    void do_layout();
    // end wxGlade

protected:
    // begin wxGlade: GenaFrame::attributes
    wxStatusBar* frame_3_statusbar;
    wxStaticText* label_1;
    wxListCtrl* list_ctrl_info;
    wxPanel* window_2_pane_1;
    wxStaticText* label_6;
    wxTextCtrl* text_ctrl_state;
    wxStaticText* label_7;
    wxTextCtrl* text_ctrl_sid;
    wxButton* button_1;
    wxStaticText* label_8;
    wxTextCtrl* text_ctrl_timeout;
    wxButton* button_2;
    wxButton* button_3;
    wxButton* button_4;
    wxStaticText* label_5;
    wxListCtrl* list_ctrl_result;
    wxScrolledWindow* window_2_pane_2;
    wxSplitterWindow* window_2;
    wxListCtrl* list_ctrl_event;
    wxPanel* window_4_pane_1;
    wxListBox* list_box_variable;
    wxPanel* window_4_pane_2;
    wxSplitterWindow* window_4;
    wxStaticText* label_10;
    wxStaticText* label_11;
    wxListBox* list_box_instance;
    wxListCtrl* list_ctrl_attribute;
    wxScrolledWindow* panel_1;
    wxTextCtrl* text_ctrl_value;
    wxPanel* notebook_1_pane_1;
    wxTextCtrl* text_ctrl_xml;
    wxNotebook* notebook_1;
    wxSplitterWindow* window_3;
    wxSplitterWindow* window_1;
    // end wxGlade

    DECLARE_EVENT_TABLE();

    void SetValues();
    void SetEvent(GenaEvent *event);
    void SetCurrent(const char *sid, const char *timeout);
    void Execute(GenaSubType type);

    long idx_udn;
    long idx_fname;
    long idx_dtype;
    long idx_stype;
    long idx_serviceId;
    long idx_eventSubURL;
    long idx_callbackURL;
    long idx_sid;
    long idx_timeout;

    wxString serviceType;
    wxString action_name;
    wxString eventSubURL;
    wxString eventSubPath;
    wxString eventSubAddr;
    wxString eventSubHost;
    uint16_t eventSubPort;
    wxString callbackURL;
    const struct tansu_cp_device *cp_device;
    const struct tansu_cp_service *cp_service;

    struct evhttp *http;
    uint16_t http_port;

    struct evhttp_connection *conn;
    wxString currentSID;
    int m_timeout;
    struct event timeout_ev;
    struct event_base *evbase;

    GenaEventList events;

public:
    virtual void OnSidReset(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnSub(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnResub(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnUnsub(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnEventDeselected(wxListEvent &event); // wxGlade: <event_handler>
    virtual void OnEventSelected(wxListEvent &event); // wxGlade: <event_handler>
    virtual void OnVarList(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnInstanceList(wxCommandEvent &event); // wxGlade: <event_handler>
    virtual void OnAttrDeselected(wxListEvent &event); // wxGlade: <event_handler>
    virtual void OnAttrSelected(wxListEvent &event); // wxGlade: <event_handler>

    void Tick();
    void HandleEvent(struct evhttp_request *req);
    void GenaHandler(struct evhttp_request *req, GenaSubType type);

    static uint16_t GetPort(int fd);
    static char *GetOutAddress(const char *remote);

}; // wxGlade: end class


#endif // GENAFRAME_H
